package com.situ.wms.util;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Map.Entry;import java.util.concurrent.ExecutionException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;

public class BaseController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action= request.getParameter("action");
		Method method=null;
		Method[] methods=this.getClass().getDeclaredMethods();//通过反射获取当前类的所有方法
		for(Method m:methods) {
		if(m.getName().equals(action)) {//重载不好用
			method=m;
		}
		}
		//执行方法 
		try {
			invokeMethod(method,request,response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	//接受一个方法使用反射技术执行方法
	protected void invokeMethod(Method m,HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, ParseException {
		//int count=m.getParameterCount();//获取m方法有几个参数
		Parameter[] parameters=m.getParameters();//获取方法的参数列表
		Class<?>[] types=m.getParameterTypes();//获取m方法所有参数的类型
		Object[] params=new Object[types.length];
		for(int i=0;i<types.length;i++) {
			Parameter p=parameters[i];//当次循环对应的那个参数
			String pName =p.getName();//获取这个参数的参数名
			if(types[i].equals(HttpServletRequest.class)) {
				params[i]=request;
			}else if(types[i].equals(HttpServletResponse.class)) {
				params[i]=response;
			}else {
//				String value=request.getParameter(pName);
//				params[i]=value;
				if(types[i].equals(Integer.class)) {
					String value=request.getParameter(pName);
					if(value!=null&&!"".equals(value)) {
						params[i]=Integer.parseInt(value);
					}
				}else if(types[i].equals(String.class)) {
					params[i]=request.getParameter(pName);
				}else if(types[i].equals(Double.class)) {
					String value=request.getParameter(pName);
					if(value!=null&&!"".equals(value)) {
						params[i]=Double.parseDouble(value);
					}
				}else if(types[i].equals(Date.class)) {
					String value=request.getParameter(pName);
					if(value!=null&&!"".equals(value)) {
						
						params[i]=value;
					}
				}else {//前面参数类型都不是，我认识应该是一个我们自己定义对象类型
					params[i]=makeObject(request,types[i]);
				}
			}
		}
		
		Object result =m.invoke(this,params);
		if(result != null) {
			response.setContentType("application/json;charset=UTF-8");
			try {
				response.getWriter().write(JSON.toJSONString(result));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private Object makeObject(HttpServletRequest req,Class<?> type) {
		try {
			Object o =type.newInstance();
		Enumeration<String> names=req.getParameterNames();//获取请求参数的名字
		while(names.hasMoreElements()) {//循环枚举所有的名字，返回false代表没下一个了
			String name =names.nextElement();//获取当前这次循环的名字
			String value=req.getParameter(name);//具体请求参数的值
			try {
			Field f=type.getDeclaredField(name);
			f.setAccessible(true);
			if(f.getType().equals(String.class)) {
				f.set(o, value);
			}else if(f.getType().equals(Integer.class)) {
				if(value!=null&&!"".equals(value))
				{
					f.set(o, Integer.parseInt(value));
				}
			}else if(f.getType().equals(Date.class)) {
				if(value!=null&&!"".equals(value)) {
					
					f.set(o, value);
				}
			}
			}catch(Exception e) {
			}
		}
		return o;
		}
		catch(Exception e) {
			return null;
		}
		}

}
